<?php

namespace JLPay\Services\Trans\Union;

use JLPay\Core\OrgConfig;
use JLPay\Services\BaseService;
use JLPay\Models\Trans\Union\UnionJsPayRequest;
use JLPay\Models\Trans\Union\UnionJsPayResponse;

/**
 * 银联JS支付服务
 */
class UnionJsPayService extends BaseService
{
    /**
     * 构造函数
     *
     * @param OrgConfig $config 配置对象
     */
    public function __construct(OrgConfig $config)
    {
        parent::__construct($config);
    }

    /**
     * 获取API URL
     *
     * @param string $requestClass 请求类名
     * @param string|null $apiPath 接口路径
     * @return string 完整的API URL
     */
    public function getApiUrl(string $requestClass, ?string $apiPath = null): string
    {
        $baseUrl = $this->config->getUrl();
        
        if ($apiPath) {
            return $baseUrl . $apiPath;
        }
        
        // 直接使用Request类中定义的API路径
        if (method_exists($requestClass, 'getApiPath')) {
            // 创建临时实例来调用getApiPath方法
            $tempInstance = new $requestClass();
            $apiPath = $tempInstance->getApiPath();
            if ($apiPath) {
                return $baseUrl . $apiPath;
            }
        }
        
        // 如果没有定义API路径，使用默认路径
        return $baseUrl . "/open/trans";
    }

    /**
     * 银联JS支付接口
     *
     * @param UnionJsPayRequest $request 银联JS支付请求
     * @return UnionJsPayResponse 银联JS支付响应
     */
    public function unionJsPay(UnionJsPayRequest $request): UnionJsPayResponse
    {
        return $this->post($request, UnionJsPayResponse::class);
    }
} 